package com.weibo.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import com.email.util.EmailMessage;
import com.email.util.SmtpEmail;
import com.weibo.db.DataBaseConnection;
import com.weibo.entity.JianceGuanjianzi;
import com.weibo.entity.JianceMail;
import com.weibo.entity.JianceWeiboText;
import com.weibo.util.WeiboHelper;

public class JianceService extends  DataBaseConnection{

	
	public void jianceSendMail(){
				//当天日期
				int y,m,d,h,mi,s;      
				Calendar cal=Calendar.getInstance();    
				y=cal.get(Calendar.YEAR);    
				m=cal.get(Calendar.MONTH);    
				d=cal.get(Calendar.DATE);    
				h=cal.get(Calendar.HOUR_OF_DAY);    
				mi=cal.get(Calendar.MINUTE);    
				s=cal.get(Calendar.SECOND);    
				System.out.println("现在时刻是"+y+"年"+m+"月"+d+"日"+h+"时"+mi+"分"+s+"秒");    
				JianceService js = new JianceService();
				List<JianceWeiboText> wbs = js.list();
				String text = "";
				for(JianceWeiboText wb : wbs){
					text = text + "<tr style='font-size: 16px'><td colspan='2' valign='middle'>【"+wb.getWeibo_name()+"】 "+wb.getText()+" <br/>" +
					   		"<a href='http://weibo.com/"+wb.getWeibo_id()+"/"+wb.getUrl()+"' style='color:#0000CC; text-decoration:underline;'>"+wb.getFaburiqi()+"</a><br>" +
					   	"</td>"+"</tr><tr ><td>&nbsp;</td></tr>";
			   	}
				System.out.println(text);
				JianceMailService ms = new JianceMailService();
				List<JianceMail> mails = ms.list();
				for(JianceMail mail : mails){
					EmailMessage emailMessage = new EmailMessage();
					StringBuffer content = new StringBuffer();
					content.append("<table width='700' border='0' cellspacing='0' cellpadding='0'><tbody>"+
								   "<tr><td colspan='2'><h1><img src='http://ww4.sinaimg.cn/mw690/c7356cd4jw1e5qd4on7bpj20jg021t8t.jpg' border='0'/></h1></td></tr><tr>"+
								   	"<td height='40' colspan='2' valign='middle'><p style='color: #fe6007'><strong>dear "+mail.getName()+"， <br />"+
								   	"【发送时间"+m+"月"+d+"日"+h+"时"+mi+"分"+"，监测时间："+m+"月"+d+"日"+" 00:00-24:00】</strong></p><br></td></tr>" +
									text+
									"<tr><td height='35' colspan='2' style='border-top:1px solid #EDEDED;padding-left:10px;'><span style='font-size:14px; font-weight:bold; color:#f60;'>万里云信息管理系统</span></td></tr>"+
									"</tbody></table>");
					emailMessage.setContent(content.toString());
					emailMessage.setFrom("nestor@aliyun.com");
					emailMessage.setPassword("wo4heike");
					emailMessage.setPort(25);
					emailMessage.setServer("smtp.aliyun.com");
					emailMessage.setSubject(m+"/"+d+"daily report321");
					emailMessage.setTo(mail.getMail());
					emailMessage.setUserName("nestor@aliyun.com");
					SmtpEmail smtpEmail = new SmtpEmail();
					smtpEmail.sendEmail(emailMessage);
				}
				
				
				
	}
		
	public List<Object> findAll(){
		
		JianceGuanjianziService service = new JianceGuanjianziService();
		List<JianceGuanjianzi> gs = service.list();
		
		String sql = "select * from t_jiance_text where text like '%淘宝%'";
		
		for(JianceGuanjianzi gjz:gs){
			sql = sql + " OR '%"+gjz.getGuanjianzi()+"%' ";
		}
		
		System.out.println("sql:" + sql);
		ResultSet rs=null;
		
		List<Object> list = new LinkedList<Object>();
		
		try {
			rs = super.getResultSet(sql, null);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		JianceWeiboText wb = null;
		try {
			while(rs.next()){
				wb = new JianceWeiboText();
				wb.setWeibo_id(rs.getInt("weibo_id"));
				wb.setFaburiqi(rs.getDate("faburiqi"));
				wb.setImg_url(rs.getString("img_url"));
				wb.setText(rs.getString("text"));
				wb.setWeibo_name(rs.getString("weibo_name"));
				wb.setPinglunshu(rs.getInt("pinglunshu"));
				wb.setZhuanfashu(rs.getInt("zhuanfashu"));
				Long tid = rs.getLong("text_id");
				wb.setText_id(tid);
				wb.setUrl(WeiboHelper.mid2url(tid+""));
				list.add(wb);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally{
			super.closeAll();
		}
		return list;
	}
	
	
public List<JianceWeiboText> list(){
		
	java.util.Date date7=new java.util.Date();
	Calendar calendar = Calendar.getInstance();
	calendar.setTime(date7);
	calendar.add(Calendar.DAY_OF_MONTH, -1);
	date7 = calendar.getTime();
	SimpleDateFormat df7 = new SimpleDateFormat("yyyy-MM-dd");
	String time2 = df7.format(date7);//倒退七天
	
		JianceGuanjianziService service = new JianceGuanjianziService();
		List<JianceGuanjianzi> gs = service.list();
		
		String sql = "select * from t_jiance_text where faburiqi>'"+time2+"' and text like '%淘宝%'";
		
		for(JianceGuanjianzi gjz:gs){
			sql = sql + " OR '%"+gjz.getGuanjianzi()+"%' ";
		}
		
		System.out.println("sql:" + sql);
		ResultSet rs=null;
		
		List<JianceWeiboText> list = new LinkedList<JianceWeiboText>();
		
		try {
			rs = super.getResultSet(sql, null);
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		JianceWeiboText wb = null;
		try {
			while(rs.next()){
				wb = new JianceWeiboText();
				wb.setWeibo_id(rs.getInt("weibo_id"));
				wb.setFaburiqi(rs.getDate("faburiqi"));
				wb.setImg_url(rs.getString("img_url"));
				wb.setText(rs.getString("text"));
				wb.setWeibo_name(rs.getString("weibo_name"));
				wb.setPinglunshu(rs.getInt("pinglunshu"));
				wb.setZhuanfashu(rs.getInt("zhuanfashu"));
				Long tid = rs.getLong("text_id");
				wb.setText_id(tid);
				wb.setUrl(WeiboHelper.mid2url(tid+""));
				list.add(wb);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally{
			super.closeAll();
		}
		return list;
	}
	public static void main(String[] args) {
		JianceService js = new JianceService();
		js.jianceSendMail();
		

	}

}
